using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Wicresoft.OmsClient
{
    class ContactsService
    {
        public static List<ListViewItem> GetContactsForAddressbook()
        {
            Outlook.ContactItem[] items = contacts();
            if (items != null)
            {
                List<ListViewItem> list = new List<ListViewItem>();
                foreach (Outlook.ContactItem item in items)
                {
                    if (string.IsNullOrEmpty(item.MobileTelephoneNumber))
                        continue;
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = item.FullName;
                    lvi.SubItems.Add(new ListViewItem.ListViewSubItem(lvi, item.MobileTelephoneNumber));
                    list.Add(lvi);
                }
                return list;
            }
            else
                return null;
        }

        public static void CompareContacts(string xml)
        {
            contactData servercd = XmlBuilder.XmlDeserialize<contactData>(xml);
            Outlook.ContactItem[] cis = contacts();
            foreach (contactDataContact cdc in servercd.contact)
            {
                try
                {
                    bool flag = true;
                    foreach (Outlook.ContactItem ci in cis)
                    {
                        if (cdc.name.ToLower() == ci.FullName.ToLower())
                        {
                            if (cdc.numbers != null && cdc.numbers.Length != 0)
                            {
                                ci.MobileTelephoneNumber = cdc.numbers[0];
                                ci.Save();
                                // ci.Display(true);
                                flag = false;
                                break;
                            }
                        }
                    }
                    if (flag && cdc.numbers != null && cdc.numbers.Length != 0)
                    {
                        Outlook.ContactItem newitem = (Outlook.ContactItem)App().CreateItem(Outlook.OlItemType.olContactItem);
                        newitem.FullName = cdc.name;
                        newitem.MobileTelephoneNumber = cdc.numbers[0];
                        newitem.Save();
                        //  newitem.Display(true);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Exception("ContactsService.CompareContacts ", ex);
                }
                
            }
 
        }

        private static GetContactsFromOutlook contacts;

        public static GetContactsFromOutlook Contacts
        {
            get { return ContactsService.contacts; }
            set { ContactsService.contacts = value; }
        }

        private static GetApplicationInstance app;

        public static GetApplicationInstance App
        {
            get { return ContactsService.app; }
            set { ContactsService.app = value; }
        }

        

    }

    public delegate Outlook.ContactItem[] GetContactsFromOutlook();
    public delegate Outlook._Application GetApplicationInstance();
}
